#include "mydx10gameapp.h"
#include "util_texshader.h"

//-------------------GAME IMPLEMENTATION - METHOD OVERRIDES------------------------------------------------------

/******************************************
* Implementation of our Game's Windows
* Message Pump. All types are passed to 
* the Default Windows WndProc Handler.
******************************************/
LRESULT CALLBACK CMyDX10GameApp::customWndProc(HWND pwindowhandle, UINT pmessagetype, WPARAM pwparam, LPARAM plparam)
{
	return DefWindowProc(pwindowhandle,pmessagetype,pwparam,plparam);
}
	
/******************************************
* 
******************************************/
bool CMyDX10GameApp::customInit()
{
	//Build a Parameter List for OIS to get the ID of the Window we are controlling input for.
	ParamList			tpl;
	std::ostringstream	twndid;
	twndid << (size_t)this->_obj_wndapp->getWindowHandle();
	tpl.insert(std::make_pair( std::string("WINDOW"),twndid.str()));
	
	//Construct a new Input Manager from our Parameter List
	this->_obj_inputman = InputManager::createInputSystem(tpl);
	this->_obj_keyboard = (Keyboard*)this->_obj_inputman->createInputObject(OISKeyboard,false);
	this->_obj_mouse    = (Mouse*)this->_obj_inputman->createInputObject(OISMouse,false);

	//Set up Timer
	this->_obj_timer.resetTimer();
	this->_obj_timer.startTimer();

	//Load our Texture Shader
	std::wstring tshaderfile = this->getShaderDataDir().append(L"texture.fx");
	LJMUUtilShaderTexPlain* ttexshader = new LJMUUtilShaderTexPlain();
	ttexshader->initialise(_obj_dxman->getDX10Device(),tshaderfile);

	//Load Our Sprite Char
	std::wstring tleveltexture = this->getImageDataDir().append(L"level.png");
	std::wstring tspritetexture = this->getSpriteDataDir().append(L"car.png");

	//Setup Level Object
	this->_level = new CMyGameObject();
	this->_level->initialise(ttexshader,_obj_dxman->getDX10Device(),tleveltexture,1024,768);
	this->_level->setPosition(this->getScreenWidth()/2,this->getScreenHeight()/2);
	this->_level->update(1.0f);

	//Setup Car Object
	this->_car_object = new CMyCarObject();
	this->_car_object->initialise(ttexshader,_obj_dxman->getDX10Device(),tspritetexture,96,141);
	return true;
}

/******************************************
* 
******************************************/
bool CMyDX10GameApp::customUpdate()	
{
	_obj_keyboard->capture();

	this->_obj_timer.updateTimer();
	float ttpf = this->_obj_timer.getTimePerFrame();

	if(_obj_keyboard->isKeyDown(KeyCode::KC_LEFT))
		this->_car_object->addRotation(90.0f * ttpf);
	else if(_obj_keyboard->isKeyDown(KeyCode::KC_RIGHT))
		this->_car_object->addRotation(-90.0f * ttpf);

	if(_obj_keyboard->isKeyDown(KeyCode::KC_UP))
		this->_car_object->addAcceleration(1.0f);
	else if(_obj_keyboard->isKeyDown(KeyCode::KC_DOWN))
		this->_car_object->addAcceleration(-1.0f);
	else
		this->_car_object->setAcceleration(0);

	this->_car_object->update(ttpf);
	return true;
}

/******************************************
* 
******************************************/
bool CMyDX10GameApp::customRender()
{
	this->_level->render(1.0f,this->_dx_mat_view,this->_dx_mat_ortho);
	this->_car_object->render(1.0f,this->_dx_mat_view,this->_dx_mat_ortho);
	return true;
}

/******************************************
* 
******************************************/
bool CMyDX10GameApp::customCleanup()
{
	this->_car_object->cleanup();
	return true;
}